JavaScript syntax
part 8/30 Β· 107.0 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
A string in JavaScript is a sequence of characters. In JavaScript,
strings can be created directly (as literals) by placing the series of
characters between double (") or single (') quotes. Such strings must be
written on a single line, but may include escaped newline characters
(such as \n). The JavaScript standard allows the backquote character
(`, a.k.a. grave accent or backtick) to quote multiline literal
strings, as well as embedded expressions using the syntax
const greeting = "Hello, World!";
const anotherGreeting = 'Greetings, people of Earth.';
const aMultilineGreeting = `Warm regards,
John Doe.`
// Template literals type-coerce evaluated expressions and interpolate
them into the string.
const templateLiteral = `This is what is stored in anotherGreeting:
${anotherGreeting}.`;
console.log(templateLiteral); // 'This is what is stored in
anotherGreeting: 'Greetings, people of Earth.''
console.log(`You are ${Math.floor(age)=>18 ? "allowed" : "not allowed"}
to view this web page`);
Individual characters within a string can be accessed using the charAt
method (provided by String.prototype). This is the preferred way when
accessing individual characters within a string, because it also works
in non-modern browsers:
const h = greeting.charAt(0);
In modern browsers, individual characters within a string can be
accessed (as strings with only a single character) through the same
notation as arrays:
const h = greeting[0];
However, JavaScript strings are immutable:
greeting[0] = "H"; // Fails.
Applying the equality operator ("==") to two strings returns true, if
the strings have the same contents, which means: of the same length and
containing the same sequence of characters (case is significant for
alphabets). Thus:
const x = "World";
const compare1 = ("Hello, " + x == "Hello, World"); // Here compare1
contains true.
const compare2 = ("Hello, " + x == "hello, World"); // Here compare2
contains ...
// ... false since the ...
// ... first characters ...
// ... of both operands ...
// ... are not of the same case.
Quotes of the same type cannot be nested unless they are escaped.
let x = '"Hello, World!" he said.'; // Just fine.
x = ""Hello, World!" he said."; // Not good.
x = "\"Hello, World!\" he said."; // Works by escaping " with \"
The String constructor creates a string object (an object wrapping a
string):
const greeting = new String("Hello, World!");
These objects have a valueOf method returning the primitive string
wrapped within them:
const s = new String("HelloΒ !");
typeof s; // Is 'object'.
typeof s.valueOf(); // Is 'string'.
Equality between two String objects does not behave as with string
primitives:
const s1 = new String("HelloΒ !");
const s2 = new String("HelloΒ !");
s1 == s2; // Is false, because they are two distinct objects.
s1.valueOf() == s2.valueOf(); // Is true.
Boolean
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ